/**
 * @author zhounan 
 * @date 2024年5月23日
 * @version V1.0
 */
package org.zn.etl.job.context;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.collections4.CollectionUtils;
import org.zn.etl.job.entity.CheckPoint;
import org.zn.etl.job.entity.EtlJob;
import org.zn.etl.job.entity.po.JobExecInstancePo;
import org.zn.etl.job.transformer.DataTransformer;

import lombok.Data;

/**
 * @author zhounan 
 * @date 2024年5月23日
 * @version V1.0
 */
@Data
public class JobExecInstanceContext extends JobExecInstancePo{

	private EtlJob job;
	private List<CheckPoint> checkPoints;
	
	private Integer checkPointFreqConut=1;
	private Integer cpoffset=0;
	
	private Map<String, Object> contextMap;
	
	private JobRunningCmd doCmd;
	
	private	DataTransformer trans;
	
	private Thread executeThread;
	
	private String	clusterJobName;
	
	public void initCheckPoints(Integer num) {
		
		LinkedList<CheckPoint> link =new LinkedList<>();
		
		if(CollectionUtils.isEmpty(checkPoints)) {
			for (int i = 0; i < num; i++) {
				CheckPoint cp=new CheckPoint();
				cp.setInsid(getId());
				link.add(cp);
			}
		}else {
			
			link.addAll(checkPoints);
			
			if(link.size()<num) {
				for (int i = 0; i < num-link.size(); i++) {
					CheckPoint cp=new CheckPoint();
					cp.setInsid(getId());
					//cp.setId(UuidUtils.generateIdString());
					link.add(cp);
				}
			}			
		}

		checkPoints=link;
	}
	
	public CheckPoint nextCheckPoints() {
		LinkedList<CheckPoint> link=(LinkedList<CheckPoint>) checkPoints;
		CheckPoint cp=link.pop();
		link.add(cp);
		return cp;
		
	} 
	
	
	public void setContextMap( Map<String, Object> contextMap) {

		this.contextMap=contextMap;
		super.setExecParam(contextMap.entrySet().stream().filter( ent ->  ( ent.getValue() instanceof String || ent.getValue() instanceof Number )  ).collect(Collectors.toMap(k-> k.toString(), v-> v)));

	}
	
	
	
}
